package com.china.bike.controller;

import com.china.bike.dto.PageBean;
import com.china.bike.entity.BikeAccount;
import com.china.bike.entity.User;
import com.china.bike.service.BikeAccountService;
import com.china.bike.service.BikeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;

@RestController
@RequestMapping(value = "/user-bike")
public class UserBikeController {

    @Autowired
    private BikeInfoService bikeInfoService;
    @Autowired
    private BikeAccountService bikeAccountService;

    @RequestMapping(value = "/remark-faulty-bike", method = RequestMethod.POST)
    public void remarkFaultyBike(@RequestParam("bikeId") String bikeId, @RequestParam("file") MultipartFile file, HttpSession session) {
//        bikeInfoService.updateBikeStatus(status);
    }

    @RequestMapping(value = "/get-bike-by-user")
    public PageBean getBikeById(@RequestParam(value = "start", required = false) Integer start,
                                @RequestParam(value = "page", required = false) Integer page,
                                @RequestParam(value = "limit", required = false) Integer limit,
                                HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        return bikeInfoService.getBikeByUserId(user.getId(), start, limit);
    }

    @RequestMapping(value = "/drive-bike", method = RequestMethod.POST)
    public String driveBike(@RequestParam("bikeId") Long bikeId, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");

        BikeAccount userAccount = bikeAccountService.getUserAccount(user.getId());
        if (userAccount.getDepositAmount() == null) {
            return "no deposit amount";
        }
        if (userAccount.getBalance().compareTo(new BigDecimal(0)) <= 0) {
            return "no balance";
        }
        bikeInfoService.driveBike(bikeId, user);
        return "success";
    }

    @RequestMapping(value = "/lock-bike", method = RequestMethod.POST)
    public void lockBike(@RequestParam("bikeId") Long bikeId, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        bikeInfoService.lockBike(bikeId, user);
    }

}
